package com.tansun.easycare.rule.common;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.io.FileUtils;

import com.tansun.rule.utils.LoggerManager;

public class JavaUnmarshaller {

    public static Object xml2Bom(String path, Class clazz) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        String xmlStr = FileUtils.readFileToString(new File(path), "UTF-8");
        return xmlString2Bom(xmlStr, clazz);

    }

    public static Object xmlString2Bom(String xmlContent, Class clazz) {

        Object object = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            object = unmarshaller.unmarshal(new StringReader(xmlContent));
        } catch (JAXBException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
            return null;
        }

        return clazz.cast(object);
    }

    public static String bom2Xml(String path, Object object, Class clazz) throws IOException {
        String content = bom2Xml(object, clazz);
        FileUtils.write(new File(path), content, "UTF-8");
        return content;
    }

    public static String bom2Xml(Object object, Class clazz) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String xml = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazz);

            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(object, baos);
            xml = baos.toString("UTF-8");
        } catch (JAXBException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
        } catch (UnsupportedEncodingException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
        } finally {
            try {
                if (baos != null)
                    baos.close();
                baos = null;
            } catch (IOException e) {
            }
        }
        return xml;
    }


    ///////////////////////////////////


    public static Object xml2Bom(String path, Class[] clazzs) throws IOException {
        File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        String xmlStr = FileUtils.readFileToString(new File(path), "UTF-8");
        return xmlString2Bom(xmlStr, clazzs);

    }

    public static Object xmlString2Bom(String xmlContent, Class[] clazzs) {

        Object object = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazzs);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            object = unmarshaller.unmarshal(new StringReader(xmlContent));
        } catch (JAXBException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
            return null;
        }

        return object;
    }

    public static String bom2Xml(String path, Object object, Class[] clazzs) throws IOException {
        String content = bom2Xml(object, clazzs);
        FileUtils.write(new File(path), content, "UTF-8");
        return content;
    }

    public static String bom2Xml(Object object, Class[] clazzs) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String xml = null;
        try {
            JAXBContext context = JAXBContext.newInstance(clazzs);

            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(object, baos);
            xml = baos.toString("UTF-8");
        } catch (JAXBException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
        } catch (UnsupportedEncodingException e) {
            LoggerManager.error(e.getMessage(), e, JavaUnmarshaller.class);
        } finally {
            try {
                if (baos != null)
                    baos.close();
                baos = null;
            } catch (IOException e) {
            }
        }
        return xml;
    }

    public static void main(String[] args) {
        System.out.println("ffffffffffffffffffff");

    }
}
